﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : TianTeng
// Created          : 09-03-2018
//
// Last Modified By : TianTeng
// Last Modified On : 09-03-2018
// ***********************************************************************
// <copyright file="ProjectImportCmd.cs" company="北京智帆高科科技有限公司">
//     Copyright © 北京智帆高科科技有限公司 2018
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OSS.BIM.Business.Utility;
using OSS.BIM.Model;
using OSS.BIM.Utility;
using ZFGK.Addins;
using ZFGK.IO;
using ZFGK.osgEarth.UI;
using ZFGK.Utility;
using ZFGK.WinForms.Utility;

namespace OSS.BIM.Commands.ProjcetCmds
{
    /// <summary>
    /// Class ProjectImportCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class ProjectImportCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var mainForm = cmdData.MainForm as Form;
            var osgEarthViewForm = cmdData.ViewForm as OsgEarthViewForm;
            if (osgEarthViewForm == null)
                return CmdResult.Failure;
            var osgEarthViewCtrl = osgEarthViewForm.OsgEarthViewCtrl;
            var osgEarthObj = osgEarthViewCtrl.OsgEarthObj;
            var actionUtility = osgEarthViewCtrl.ActionUtility;

            var project = cmdData.Tag as Project;
            if (project == null)
                return CmdResult.Cancel;

            // 选择导入目录
            string fileListPath;
            if (!DialogUtil.OpenText(out fileListPath))
            {
                return CmdResult.Cancel;
            }
            var lines = File.ReadAllLines(fileListPath);


            var modelGropes = new List<ModelGrope>();
            var modelElements = new List<ModelElement>();
            var modelFiles = new List<ModelFile>();
            var modelFileBinarys = new List<ModelFileBinary>();
            var modelPropertys = new List<ModelProperty>();

            ZfUiProgressUtil.SetProgress("导入项目");
            int index = 0;
            foreach (var line in lines)
            {
                ZfUiProgressUtil.SetProgress(index++, lines.Length);

                var items = line.Split(" ");
                if (items.Length == 0)
                    continue;
                
                var filePath = items[0];
                var modelGroupName = items.Length > 1 ? items[1] : "默认分组";
                var modelGrope = modelGropes.FirstOrDefault(directory => directory.Name == modelGroupName);
                if (modelGrope == null)
                {
                    modelGrope = new ModelGrope();
                    modelGrope.NewIdIfNotSet();
                    modelGrope.ProjectId = project.ProjectId;
                    modelGrope.Name = modelGroupName;
                    modelGropes.Add(modelGrope);
                }

                string xmlName = filePath.ChangeExtension(".xml");
                if (!File.Exists(xmlName))
                    continue;

                var xmlStr = FileUtil.Read(xmlName, Encoding.UTF8);

                var modelElement = new ModelElement();
                modelElement.NewIdIfNotSet();

                try
                {
                    var properties = XmlSerializerUtil.DeserializeStr<XmlDictionary<string, string>>(xmlStr);

                    if (properties.ContainsKey("元素名称"))
                    {
                        modelElement.Name = properties["元素名称"];

                        foreach (var property in properties)
                        {
                            var modelProperty = new ModelProperty();
                            modelProperty.NewIdIfNotSet();
                            modelProperty.ModelElementId = modelElement.ModelElementId;
                            modelProperty.Name = property.Key;
                            modelProperty.DiaplayLable = property.Key;
                            modelProperty.Value = property.Value;
                            modelProperty.ValueType = 0;
                            modelPropertys.Add(modelProperty);

                        }
                    }
                    else continue;
                }
                catch { continue; }

                if (modelElement.Name == "标准")
                    continue;

                modelElement.ModelGropeId = modelGrope.ModelGropeId;
                modelElements.Add(modelElement);

                var modelFile = new ModelFile();
                modelFile.NewIdIfNotSet();
                modelFile.ModelElementId = modelElement.ModelElementId;
                modelFile.Name = Path.GetFileNameWithoutExtension(filePath);
                modelFile.Extension = Path.GetExtension(filePath);
                modelFile.Loaded = true;
                modelFiles.Add(modelFile);

                var modelFileBinary = new ModelFileBinary();
                modelFileBinary.ModelFileId = modelFile.ModelFileId;
                modelFileBinary.File = File.ReadAllBytes(filePath);
                modelFileBinarys.Add(modelFileBinary);
                
            }
            ZfUiProgressUtil.ClearProgress();

            // 更新数据库
            // 更新模型树
            // 更新场景
//             foreach (var modelGrope in modelGropes)
//             {
//                 var treeListNode = modelGrope.AddOrUpdateTreeListNode(false);
//             }
//             osgEarthObj.AddOrReplaceGroup(modelGrope);
//             osgEarthObj.AddOrReplaceModel(modelInfos);
            return CmdResult.Succeed;
        }
    }
}
